/**
 * 
 */
package com.alphasystem.commerce.common.util;

import java.lang.reflect.Method;

/**
 * @author Syed Farhan Ali
 * 
 */
public class ReflectionHelper {

	public static String findGetterMethodName(String property) {
		return "get" + String.valueOf(property.charAt(0)).toUpperCase()
				+ property.substring(1);
	}

	@SuppressWarnings("rawtypes")
	public static Method findMethod(Class entityClass, String property) {
		Method method = null;
		Method[] declaredMethods = entityClass.getMethods();
		for (Method declaredMethod : declaredMethods) {
			String methodName = declaredMethod.getName();
			if (methodName.equals(findSetterMethodName(property))) {
				method = declaredMethod;
				break;
			}
		}
		return method;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Method findMethod(Class c, String methodName,
			Class... parameterTypes) {
		Method method = null;
		try {
			method = c.getMethod(methodName, parameterTypes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return method;
	}

	@SuppressWarnings({ "rawtypes" })
	public static Method findSetterMethod(Class c, String property,
			Class... parameterTypes) {
		return findMethod(c, findSetterMethodName(property), parameterTypes);
	}

	public static String findSetterMethodName(String property) {
		return "set" + String.valueOf(property.charAt(0)).toUpperCase()
				+ property.substring(1);
	}

}
